#include "stdafx.h"
#include "MyControls/PropertyString.h"
#include <MyFC/LabelledWidget.h>

namespace My {

// RowPropertyControl

RowPropertyControl::RowPropertyControl(const PropertyMap & map, const EditableList & list)
{
	SetGap(0);
	SetBorderX(4);
	PropertyMap::const_iterator it;
	for (it = map.begin(); it != map.end(); ++it) {
		LabelledWidget * pChild = new LabelledWidget();
		pChild->SetCaption(it->first + ":");
		pChild->SetLayout(LabelledWidget::CaptionLeft);
		pChild->SetWidget((* it).second->CreateControl(list));
		AppendChild(pChild);
	}
}

RowPropertyControl::~RowPropertyControl()
{
	for (Size i = 0; i < GetSize(); ++i) {
		LabelledWidget * pChild = dynamic_cast<LabelledWidget *>(GetChild(i));
		assert(pChild != 0);
		SetChild(i, 0);
		delete pChild->GetWidget();
		delete pChild;
	}
}

// PropertyString

bool PropertyString::Accept(const EditableObject & obj) const 
{ 
	for (const_iterator it = begin(); it != end(); ++it)
		if (!(* it).second->Accept(obj))
			return false;
	return true;
}

bool PropertyString::ReadOnly(const EditableList & list) const
{
	for (const_iterator it = begin(); it != end(); ++it)
		if (!(* it).second->ReadOnly(list))
			return false;
	return true;
}

bool PropertyString::Visible(const EditableList & list) const
{
	for (const_iterator it = begin(); it != end(); ++it)
		if ((* it).second->Visible(list))
			return true;
	return false;
}

Widget * PropertyString::CreateControl(const EditableList & list)
{
	return new RowPropertyControl(* this, list);
}

void PropertyString::FreeControl(Widget * pWidget)
{
	delete pWidget;
}

} //namespace My
